﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Logistics
{
    public partial class frmOwiFileModify : Form
    {
        public delegate void ModelEventHandler(Model.TC_OWI_FILE model);
        public ModelEventHandler OnModelEvent;
        private Model.TC_OWI_FILE owi_file;
        public frmOwiFileModify()
        {
            InitializeComponent();
        }
        public frmOwiFileModify(Model.TC_OWI_FILE _owi_file) : this()
        {
            owi_file = _owi_file;
        }

        private void butSave_Click(object sender, EventArgs e)
        {
            if (!VerifyFrom())
                return;
            var model = new Model.TC_OWI_FILE();
            model.TC_OWI001 = txtOwi001.Text.Trim();
            model.TC_OWI002 = txtOwi002.Text.Trim();
            model.TC_OWI007 = cmbOwi007.SelectedIndex.ToString();
            model.TC_OWI008 = txtOwi008.Text.Trim();
            var msg = "确定要保存'" + txtOwi002.Text + "'吗？";
            if (MessageBox.Show(msg, "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
            {
                var objOwiFile = new Manager.TC_OWI_FILE();
                if (owi_file == null)
                {
                    objOwiFile.Add(model);
                }
                else
                {
                    model.TC_OWI009 = owi_file.TC_OWI009;
                    objOwiFile.Update(model);
                }
                OnModelEvent?.Invoke(model);
                MessageBox.Show("数据保存成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Close();
            }
        }
        private bool VerifyFrom()
        {
            var msg = string.Empty;
            if (string.IsNullOrEmpty(txtOwi001.Text.Trim()))
            {
                msg = "编码不能为空";
                MessageBox.Show(msg, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtOwi002.Focus();
                return false;
            }

            if (string.IsNullOrEmpty(txtOwi002.Text.Trim()))
            {
                msg = "物流公司名称不能为空";
                MessageBox.Show(msg, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtOwi002.Focus();
                return false;
            }
            if (cmbOwi007.SelectedIndex == 0)
            {
                msg = "请选择导出样式";
                MessageBox.Show(msg, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                cmbOwi007.Focus();
                return false;
            }
            if (string.IsNullOrEmpty(txtOwi008.Text.Trim()))
            {
                msg = "物流代号不能为空";
                MessageBox.Show(msg, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtOwi008.Focus();
                return false;
            }
            return true;
        }
        private void butCancel_Click(object sender, EventArgs e)
        {
            var msg = "数据尚未保存，确认要取消吗？";
            if (MessageBox.Show(msg, "警告", MessageBoxButtons.YesNo,
                MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
            {
                this.Close();
            }
        }

        private void frmOwiFileModify_Load(object sender, EventArgs e)
        {
            if (owi_file != null)
                DataBind();
        }
        private void DataBind()
        {
            txtOwi001.ReadOnly = true;
            txtOwi001.Text = owi_file.TC_OWI001;
            txtOwi002.Text = owi_file.TC_OWI002;
            if (!string.IsNullOrWhiteSpace(owi_file.TC_OWI007))
                cmbOwi007.SelectedIndex = Convert.ToInt32(owi_file.TC_OWI007);
            txtOwi008.Text = owi_file.TC_OWI008;
        }
    }
}
